﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class UserService : IUserService
    {
        private Dictionary<string, string> loginUsers = new Dictionary<string, string>();

        public string Login(string username, string password)
        {
            string sessionId = OperationContext.Current.SessionId;
            if (!string.IsNullOrEmpty(username) && password == "123")
            {
                if (loginUsers.ContainsKey(sessionId))
                {
                    if (loginUsers[sessionId] == username)
                        return string.Format("您已经登录，不需要重复登录！", username);
                    else
                    {
                        loginUsers[sessionId] = username;
                        return string.Format("更换登录用户完毕");
                    }
                }
                else
                {
                    if (!loginUsers.ContainsValue(username))
                    {
                        loginUsers.Add(sessionId, username);
                        return "登录完成";
                    }
                    else
                    {
                        return string.Format("用户{0}已在其它地方有登录，同一时间不允许同一用户重复登录！", username);
                    }
                }
            }
            else
            {
                return "用户名或密码错误！";
            }
        }

        public void LogOut()
        {
            string sessionId = OperationContext.Current.SessionId;
            loginUsers.Remove(sessionId);
        }

        public void Register(UserRegisterModel user)
        {
            throw new NotImplementedException();
        }
    }
}
